
package com.shanjupay.merchant.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.List;
/**
 * <p>
 * RestTemplate 配置类
 * 1. 指定底层实现使用OkHttpClient
 * 2. 设置字符串转换器，字符集UTF8（默认ISO_8859_1）
 * </p>
 */
@Configuration
public class RestTemplateConfig {
    /**
     * 字符串消息转换器索引值
     */
    public static final int STRING_MESSAGE_CONVERTER_INDEX = 1;
    //@Bean(name = "restTemplate")
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
       /* OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory();
        RestTemplate restTemplate = new RestTemplate(factory);
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        // 防止中文乱码
        messageConverters.set(STRING_MESSAGE_CONVERTER_INDEX, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;*/
    }
}